﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;

namespace CygnusGroundStation
{
	/// <summary>
	/// Interaction logic for AddModule.xaml
	/// </summary>
	public partial class AddModuleDialog : Window
	{
		public AddModuleDialog()
		{
			InitializeComponent();
		}

		private void bAdd_Click(object sender, RoutedEventArgs e)
		{
			foreach (ModuleInfo module in lbModules.SelectedItems)
			{
				AddModule(module);
			}
		}

		private void lbModules_MouseDoubleClick(object sender, MouseButtonEventArgs e)
		{
			foreach (ModuleInfo module in lbModules.SelectedItems)
			{
				AddModule(module);
			}

			Close();
		}

		private void AddModule(ModuleInfo in_module)
		{
			string[] modules = SystemSettings.Default.GetSections();
			string name;
			int index;

			// check if this module already exists
			index = 0;
			name = in_module.DLLName;

			while (Array.IndexOf(modules, name) != -1)
			{
				if (SystemSettings.Default.ReadBool(name, "Active", false))
				{
					index++;

					name = in_module.DLLName + "_" + index.ToString();
				}
				else
					break;
			}

			// add module to config
			SystemSettings.Default.WriteBool(name, "Active", true);
		}
	}
}

